package soar.test.thread.notify;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Sender extends Thread
{
    private final static Logger logger = LoggerFactory.getLogger(Sender.class);

    private Receiver[] receivers;

    public Sender(Receiver[] receivers) throws Exception
    {
        this.receivers = receivers;
    }

    public void run()
    {
        int receiver_idx = 0;
        long count = 0;
        while (true)
        {
            try
            {
                Request request = new Request();
                request.id = count;
                request.name = "";

                receivers[receiver_idx].put(request);
                ++receiver_idx;
                if (receiver_idx == receivers.length)
                    receiver_idx = 0;

                ++count;
                if (count % 10000 == 0)
                {
                    logger.info("send: {}", count);
                    Thread.sleep(1 * 1000);
                }
            }
            catch (Exception e)
            {
                logger.info("send fail: {}", e);
            }
        }
    }
}
